﻿using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.W.realize;

namespace MyMES.Read.API.Application.CommendHandlers.W.realize
{
    public class GetProductionreturnCommandHanders : IRequestHandler<GetProductionreturnCommands, APIPaging<List<Productionreturn>>>
    {
        private readonly IBaseRepository<Productionreturn> baseRepository;

        public GetProductionreturnCommandHanders(IBaseRepository<Productionreturn> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        /// <summary>
        /// 显示生产退料
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>7   
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<Productionreturn>>> Handle(GetProductionreturnCommands request, CancellationToken cancellationToken)
        {
            APIPaging<List<Productionreturn>> result = new APIPaging<List<Productionreturn>>();
            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";
            var sql = "select * from Productionreturn where IsDelete = false ORDER BY PId DESC";
            var list = await baseRepository.SelectAsync(sql);
            if (!string.IsNullOrEmpty(request.ReturnNumber))
            {
                list = list.Where(x => x.ReturnNumber.Contains(request.ReturnNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ReturnName))
            {
                list = list.Where(x => x.ReturnName.Contains(request.ReturnName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Productionwork))
            {
                list = list.Where(x => x.Productionwork.Contains(request.Productionwork)).ToList();
            }
            if (request.Returntype>0)
            {
                list = list.Where(x => x.Returntype == request.Returntype).ToList();
            }
            result.TotalCount = list.Count;
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            list=list.Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            result.data= list;
            return result;
        }
    }
}
